package cn.bone.excel;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ExcelUtil {

    public static List<NameData> readSheet(DataParam param){

        File file = new File(param.getFileName());
        List<NameData> list = new ArrayList<>();
        try(Workbook workbook = WorkbookFactory.create(file)) {
            Sheet sheet = workbook.getSheetAt(param.getSheetAt());
            int rowCount = sheet.getPhysicalNumberOfRows();
            System.out.println(rowCount);
            Map<String, NameData> map = new HashMap<>();
            for(int i=1; i<param.getLastRow(); i++){
                Row row = sheet.getRow(i);
//                if(row==null){
//                    System.out.println(param.getSheetAt() + " -->" + i);
//                    continue;
//                }
                String name1 = row.getCell(param.getName1At()).getStringCellValue();
                String name2 = row.getCell(param.getName2At()).getStringCellValue();
                String nameCode = row.getCell(param.getNameCodeAt()).getStringCellValue();
                String nameValue = row.getCell(param.getNameValueAt()).getStringCellValue();

//                System.out.println(i + ":\t" + name1 + "\t" + name2 + "\t" + nameCode + "\t" + nameValue);

                if(!map.containsKey(name2)){
                    NameData data = new NameData();
                    data.setName1(name1);
                    data.setName2(name2);
                    data.setNameCode(nameCode);
                    data.setNameValue(nameValue);
                    map.put(name2, data);
                    list.add(data);
                }else{
                    NameData data = map.get(name2);
                    String value = data.getNameValue() + "、" + nameValue;
                    data.setNameValue(value);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }

    public static void createSheet(XSSFSheet sheet, List<NameData> list){

        XSSFRow rowTitle = sheet.createRow(0);
        XSSFCell cellTitle0 = rowTitle.createCell(0);
        cellTitle0.setCellValue("院校代码");
        XSSFCell cellTitle1 = rowTitle.createCell(1);
        cellTitle1.setCellValue("院校名称");
        XSSFCell cellTitle2 = rowTitle.createCell(2);
        cellTitle2.setCellValue("院校名称");
        XSSFCell cellTitle3 = rowTitle.createCell(3);
        cellTitle3.setCellValue("专业名称");
        for(int i=0; i<list.size(); i++){
            NameData data = list.get(i);
            XSSFRow row = sheet.createRow(i + 1);
            XSSFCell cell0 = row.createCell(0);
            cell0.setCellValue(data.getNameCode());
            XSSFCell cell1 = row.createCell(1);
            cell1.setCellValue(data.getName1());
            XSSFCell cell2 = row.createCell(2);
            cell2.setCellValue(data.getName2());
            XSSFCell cell3 = row.createCell(3);
            cell3.setCellValue(data.getNameValue());
        }
    }

}
